package com.atguigu.common.exception;

import com.atguigu.common.result.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice //异常接口
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(value=Exception.class)
    @ResponseBody
      //处理全局异常
    public R error(Exception e){
        e.printStackTrace();
        return  R.error().message(e.getMessage());
    }
    //处理特定异常
    @ExceptionHandler(value = YyghException.class)
    @ResponseBody
    public R error(YyghException e){
       log.error(e.getMsg()); //打印error级别的日志
        return  R.error().message(e.getMsg());
    }
    //处理特定异常
    @ExceptionHandler(value = ArithmeticException.class)
    @ResponseBody
    public R error(ArithmeticException exp){
        log.warn("数学运算异常");
        return R.error().message(exp.getMessage());
    }



}
